﻿#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-08-15 13:23:30Z</CreationDate>
</File>
*/
#endregion

using System;

#if NETFX_CORE
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml;
#else
using System.Globalization;
using System.Windows;
using System.Windows.Data;
#endif

namespace Outcoder.UI.Xaml.Converters
{
	/// <summary>
	/// Converts a boolean value to a <see cref="Visibility"/> value, 
	/// and vice versa.
	/// </summary>
	public class BooleanToVisibilityConverter : IValueConverter
	{
		object Convert(object value, Type targetType, object parameter)
		{
			string paramValue = (string)parameter ?? string.Empty;

			bool collapsedParamPresent = string.Compare(
				paramValue.Trim(), "collapsed", StringComparison.OrdinalIgnoreCase) == 0;


			if (value == null || (bool)value)
			{
				return collapsedParamPresent ? Visibility.Collapsed : Visibility.Visible;
			}

			return collapsedParamPresent ? Visibility.Visible : Visibility.Collapsed;
		}

		object ConvertBack(object value, Type targetType, object parameter)
		{
			string paramValue = (string)parameter ?? string.Empty;

			bool collapsedParamPresent = string.Compare(
					paramValue.Trim(), "collapsed", StringComparison.OrdinalIgnoreCase) == 0;

			if (value == null || (Visibility)value == Visibility.Visible)
			{
				return !collapsedParamPresent;
			}

			return collapsedParamPresent;
		}

#if !NETFX_CORE
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return Convert(value, targetType, parameter);
		}

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return ConvertBack(value, targetType, parameter);
		}
#else
		public object Convert(object value, Type targetType, object parameter, string language)
		{
			return Convert(value, targetType, parameter);
		}

		public object ConvertBack(object value, Type targetType, object parameter, string language)
		{
			return ConvertBack(value, targetType, parameter);
		}
#endif

	}
}